#ifndef _FILE_H
#define _FILE_H


#include <unistd.h>
#include "types.h"
#include <sys/stat.h>
#include "proc.h"
typedef struct file
{
  int kfd; // file descriptor on the host side of the HTIF
  uint32 refcnt;
  short major; // 设备编号
  short minor; // 同个设备不同部分编号
} file_t;

extern file_t files[];

typedef struct devi {
    char path[MAXPATH];
    short (*open)();
    ssize_t (*read)(short, void *, size_t);
    ssize_t (*write)(short,const void *, size_t);
    void (*close)(short);
} devi_t;

#define O_RDONLY	     00
#define O_WRONLY	     01
#define O_RDWR		     02
#define	ENOMEM		12	/* Out of memory */

#define stdin  (files + 0)
#define stdout (files + 1)
#define stderr (files + 2)

ssize_t file_pwrite(file_t* f, const void* buf, size_t size, off_t offset);
ssize_t file_write(file_t* f, const void* buf, size_t n);
file_t* file_open(const char* fn, int flags, int mode);
ssize_t file_pread(file_t* f, void* buf, size_t n, off_t off);
void file_decref(file_t* f);
void file_init();
file_t* file_get(int fd);
ssize_t file_lseek(file_t* f, size_t ptr, int dir);
ssize_t file_read(file_t* f, void* buf, size_t size);
file_t* file_open(const char* fn, int flags, int mode);
file_t* file_openat(int dirfd, const char* fn, int flags, int mode);
int file_dup(file_t* f);
int fd_close(int fd);
int file_truncate(file_t* f, off_t len);
int file_stat(file_t* f, struct stat* s);
void printProcFiles(struct proc * p);
int fd_proc_close(int fd,struct proc * p);
file_t* file_proc_get(int fd,struct proc * p);
#endif
